package com.example.streamresponse.controller;

import com.example.streamresponse.dto.CreateBalanceWebRequest;
import com.example.streamresponse.entity.Balance;
import com.example.streamresponse.repository.BalanceRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@Tag(name = "账户信息")
@RestController
@RequestMapping("/api/balance")
@RequiredArgsConstructor
@Slf4j
public class BalanceController {
    private final BalanceRepository balanceRepository;

    @Operation(summary = "创建账户")
    @PostMapping
    public Mono<Balance> createBalance(@RequestBody CreateBalanceWebRequest request) {
        String userCode =RandomStringUtils.randomAlphabetic(7);
        Long updateDate = System.currentTimeMillis();
        final Balance balance = Balance.builder()
                .balance(request.getBalance())
                .memberId(request.getMemberId())
                .updatedBy(userCode)
                .createdBy(userCode)
                .createdDate(updateDate)
                .lastModifiedBy(userCode)
                .lastModifiedDate(System.currentTimeMillis())
                .build();
        return balanceRepository.save(balance);
    }
}
